package com.tsf.demo.tools.service;

import com.tencent.cloud.task.sdk.core.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * <p><em>Created on 2022/3/18 10:41 下午</em></p>
 *
 * @author fuzhongtang
 * @since 1.0.1
 */
@Service
@EnableScheduling
public class DiscoveryClientSerivce {

    @Autowired
    private DiscoveryClient discoveryClient;
    public static long isTestTime = System.currentTimeMillis();
    public static long count = 0;

    @Scheduled(cron = "0/5 * * * * ? ")
    public synchronized void watchRegister()
    {
        count++;
        Register.listLog.add("0000000----watchRegister...count="+count);

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true)
                {
                    flushServerList();

                    if (count<900000000)
                    {
                        count++;
                    }
                    else
                    {
                        count = 100;
                    }
                    if ((System.currentTimeMillis() - isTestTime) < 1000*60*20)
                    {
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException e) {
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }).start();
    }

    public synchronized  void flushServerList() {
        Register.listLog.clear();
        List<String> list = discoveryClient.getServices();
        Register.listLog.add(DateUtil.getCurrentDateStr()+"=discoveryClient.getServices="+list.size());

        for (String serviceId:list) {
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("for 0 "+serviceId+" \n ");
            if ((""+serviceId).contains(Register.testName))
            {
                stringBuffer.append("for 1 \n ");

                List<ServiceInstance> listService = discoveryClient.getInstances(serviceId);
                if (listService.size()>0)
                {
                    ServiceInstance serviceInstance = listService.get(0);

                    Register.register(serviceInstance);
                    stringBuffer.append("for 2 \n ");
                }
                else
                {
                    stringBuffer.append("for 3 \n ");
                }
                Register.listLog.add(stringBuffer.toString());
            }
            else
            {
                stringBuffer.append("for 4 \n ");
            }


        }

    }

}
